package com.wxzhou.wangzhuan.dto;

import jakarta.validation.constraints.Min;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

/**
 * 通用分页请求对象
 */
@Schema(description = "通用分页请求参数", example = "{\"page\":1,\"limit\":20}")
public class PageRequestDto extends CommonRequestDto {

    /**
     * 每页条数，默认10
     */
    @Schema(description = "每页条数，默认10", example = "20")
    @Min(value = 1, message = "页码必须大于0")
    private Integer limit;

    /**
     * 当前页码，默认1
     */
    @Schema(description = "当前页码，默认1", example = "1")
    @Min(value = 1, message = "每页数量必须大于0")
    private Integer page;

    public PageRequestDto() {
    }

    public Integer getLimit() {
        return limit == null || limit <= 0 ? 10 : (limit >= 100 ? 10 : limit);
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Integer getPage() {
        return page == null || page <= 0 ? 1 : page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }


    /**
     * 获取偏移量（用于数据库查询）
     */
    public int getOffset() {
        int offset = getPage() != null ? (getPage() - 1) * getLimit() : 0;
        return offset;
    }

}